* create all couple panel -5 to +5
global data_folder "W:\intimate\data"
global log_folder "W:\intimate\dofiles\logs"
global result_folder "W:\intimate\results"

local go_back = 5
local go_forward = 5

local var_list ptoim1 tyotu saiprva ika ututku sukup sphnro a18lkm seutukunta16 kunta sykstun

foreach y of numlist 2014/2016 {

		use shnro `var_list' using "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_`y'.dta", clear
		drop if missing(sphnro) // must be in couple this year
		
		merge 1:1 shnro using "$data_folder\fertility_`y'.dta"
		drop if _merge==2
		drop _merge
		
		// rename to wide format
		foreach thing in `var_list' nchild{
			ren `thing' `thing'0 // year get together
		}
		g info_missing0 = 0
		
		// must be observed in the previous period
		local y_1 = `y'-1
		merge 1:1 shnro using "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_`y_1'.dta", ///
				keepusing(`var_list')
		keep if _merge==3
		drop _merge
		
		g info_missingB1 = 0
		
		
		// keep if cohabiting in previous period
		drop if sphnro0 == sphnro
		
		// merge with children 				
		merge 1:1 shnro using "$data_folder\fertility_`y_1'.dta"
		drop if _merge==2
		drop _merge
		
		
		// Create suspect data
		foreach thing in `var_list' nchild {
			ren `thing' `thing'B1
		}
		
		forvalues j = 2/`go_back' {
			
			local y_1 = `y'-`j'
			
			merge 1:1 shnro using "$data_folder\fertility_`y_1'.dta"
			drop if _merge==2
			drop _merge
			
			merge 1:1 shnro using "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_`y_1'.dta", ///
				keepusing( `var_list')
			drop if _merge==2
			drop if sphnro == sphnro0
	
			
			foreach thing in `var_list' nchild{
				ren `thing' `thing'B`j'
			}
			g info_missingB`j' = (_merge==1)
			drop _merge
			
		}
		
		// merge in spouse info at t-1
		ren shnro person_id
		ren sphnro0 shnro // so can merge info in 
		local y_1 = `y'-1
		merge 1:1 shnro using "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_`y_1'.dta", ///
				keepusing(ptoim1 tyotu ika ututku sukup)
		drop if _merge==2
		drop _merge

		foreach thing in ptoim1 tyotu ika ututku sukup {
			ren `thing' spouse_pre_`thing'
		}
		ren shnro sphnro0
		ren person_id shnro
		
		// go forward info
		forvalues j = 1/`go_forward' {
			
			local y_1 = `y'+`j'
			if `y_1' <= 2016 {
			    
		    merge 1:1 shnro using "$data_folder\fertility_`y_1'.dta"
			drop if _merge==2
			drop _merge
			
			merge 1:1 shnro using "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_`y_1'.dta", ///
				keepusing( `var_list')
			drop if _merge==2
			

			
			foreach thing in `var_list' nchild{
				ren `thing' `thing'F`j'
			}
			g info_missingF`j' = (_merge==1)
			drop _merge
			}
			
			// need to use folk 
			if `y_1' > 2016 & `y_1' <=2019 {
	
			* Income
			merge 1:1 shnro using "W:\sexual_crime\data\FOLK_emp_earnings_edu_`y_1'.dta", keepusing(ptoim1 tyotu sykstun)
			g info_missingF`j' = (_merge == 1)
			drop if _merge ==2
			drop _merge
			
			
			* number of children
			merge 1:1 shnro using "$data_folder\folkchild_`y_1'.dta"
			drop if _merge ==2
			drop _merge vuosi
			
			* number of children
			merge 1:1 shnro using "$data_folder\fertility_`y_1'.dta"
			drop if _merge ==2
			drop _merge 
		
			* Spouse 
			g vuosi = `y_1'
			merge 1:1 shnro vuosi using "W:\intimate\data\folk_spouses.dta",  keep(1 3)
			drop if _merge==2
			drop _merge vuosi
			
			
			foreach thing in ptoim1 tyotu sphnro a18lkm nchild{
				ren `thing' `thing'F`j'
			}
			
			}
			if `y_1' > 2019 {
			    g info_missingF`j' =1
			}
		}
			
		g year_start_cohab = `y'
		order year_start_cohab, after(shnro)
		
		save "$data_folder\allcouple_data_`y'.dta", replace
}

// Clean - FLEED variables 
forvalues i = 1/5 {
	replace info_missingF`i' = 1 if missing(info_missingF`i')
}

g age = ika0
g spouse_pre_age = spouse_pre_ika

destring ututku0, replace 
g educ = floor(ututku0/100000)
replace educ = 0 if missing(ututku0)

destring spouse_pre_ututku, replace
g spouse_pre_educ = floor(spouse_pre_ututku/100000)
replace spouse_pre_educ = 0 if missing(spouse_pre_ututku) & missing(spouse_pre_ika)==0

g male = (sukup0=="1")

// employment 
destring *ptoim1*, replace
replace ptoim10 = (ptoim10==11)
replace ptoim10 =. if info_missing0==0 & missing(ptoim10)
forvalues i = 1/5 {
	replace ptoim1B`i' = (ptoim1B`i'==11)
	replace ptoim1B`i' = . if info_missingB`i'
}
forvalues i = 1/5 {
	replace ptoim1F`i' = (ptoim1F`i'==11)
	replace ptoim1F`i' = . if info_missingF`i'
}
replace spouse_pre_ptoim1 = (spouse_pre_ptoim1==11)
replace spouse_pre_ptoim1 = . if missing(spouse_pre_ptoim1) & missing(spouse_pre_ika)==0

// earnings
g year = year_start_cohab
merge m:1 year using "W:\intimate\data\cpi.dta"
keep if _merge==3
drop _merge
foreach thing in tyotu0 saiprva0 {
	replace `thing' = `thing'/cpi
	replace `thing' = 0 if missing(`thing') & info_missing0==0
}
drop cpi

forvalues i = 1/5 {
	replace year = year_start_cohab - `i'
	merge m:1 year using "W:\intimate\data\cpi.dta"
	keep if _merge==3
	drop _merge
	foreach thing in tyotu saiprva {
		replace `thing'B`i' = `thing'B`i'/cpi
		replace `thing'B`i' = 0 if missing(`thing'B`i') & info_missingB`i'==0
	}
	drop cpi
}
forvalues i = 1/5 {
	replace year = year_start_cohab + `i'
	merge m:1 year using "W:\intimate\data\cpi.dta"
	drop if _merge==2
	drop _merge
	foreach thing in tyotu saiprva {
		replace `thing'F`i' = `thing'F`i'/cpi
		replace `thing'F`i' = 0 if missing(`thing'F`i') & info_missingF`i'==0 & year~=2019 // issue with tyotu in 2019 so all missing
	}
	drop cpi
}


replace year = year_start_cohab - 1
merge m:1 year using "W:\intimate\data\cpi.dta"
keep if _merge==3
drop _merge
replace spouse_pre_tyotu = spouse_pre_tyotu/cpi
replace spouse_pre_tyotu = 0 if missing(spouse_pre_tyotu) & missing(spouse_pre_ika)==0
drop year 

replace nchild0=0 if nchild0==.
forvalues i = 1/5 {
	replace nchildF`i' = 0 if nchildF`i'==.
	replace nchildB`i' = 0 if nchildB`i'==.
}


drop *ika* *sukup* *ututku*

// Drop if age is not correct 
drop if age < 18
drop if age >= 65

sort shnro sphnro0 year_start_cohab
bys shnro sphnro0 : gen id_N = _N
bys shnro sphnro0 : egen min_y = min(year_start_cohab)
drop if id_N > 1 & year_start_cohab != min_y
drop id_* min_y
save "W:\intimate\data\allcouple_data_clean", replace


preserve
// just have panel for the men 
keep if male==1
drop male 
save "W:\intimate\data\allmen_data_clean", replace 

restore 

// just have panel for the women 
keep if male==0
drop male 
save "W:\intimate\data\allwomen_data_clean", replace

